//
//  RootViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/14.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "RootViewController.h"
#import "LoginRegisterViewController.h"

@interface RootViewController ()<UIGestureRecognizerDelegate,UIAlertViewDelegate>
{
    BOOL _isChange;
    BOOL _isH;
    BOOL gestureFaile;  //手势是否失效
    UIView *layerBack;
    UIImageView *backimage;
}

@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;

@property (nonatomic, strong) UISwipeGestureRecognizer *SwipeRecognizer;

@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGesture;

@end

@implementation RootViewController
- (void)viewWillAppear:(BOOL)animated{
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self sendAtLine];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:50.0 target:self selector:@selector(sendAtLine) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    gestureFaile = NO;
}
/**
 *  定时发送请求，让服务器知道自己在线，在这又同时判断本地密码是否有效，如果请求失败说明可能密码被修改了
 */
- (void)sendAtLine{
    NSString *ashx = @"/Server/UserHandler.ashx";
    NSDictionary *dic = @{@"act":@"keeponline",@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        [LBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (error == nil) {
            //请求成功
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
            }else{
                //请求异常
                NSLog(@"%@",model[@"msg"]);
                //点击确定响应警告框代理，执行跳转到登录界面
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码已被修改，请重新登录!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        }else{
            [XHToast showBottomWithText:@"网络断开连接"];
        }
    }];
    
}

/**
 *  重写init方法
 */
- (id)initWithCenterVC:(MainViewAndNoReadViewController *)centerVC leftVC:(DrawerViewController *)leftVC {
    
    if (self = [super init]) {
        [self addChildViewController:leftVC];
        
        UINavigationController *centerNC = [[UINavigationController alloc] initWithRootViewController:centerVC];
        [self addChildViewController:centerNC];
        
        leftVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/5*4, [UIScreen mainScreen].bounds.size.height);
        
        
        centerNC.view.frame = [UIScreen mainScreen].bounds;
        [self.view addSubview:self.backimage];
        [self.view addSubview:leftVC.view];
        [self.view addSubview:centerNC.view];
        
        centerVC.navigationItem.leftBarButtonItem = ({
            UIBarButtonItem *leftB = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction)];
            leftB;
        });
        _rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftAction)];
        // 设置轻扫手势支持的方向
        _rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        [centerNC.view addGestureRecognizer:_rightSwipeGesture];
        centerVC.leftActionBlock = ^(){
            [self leftAction];
        };
        
        centerVC.RemoveGestureRecognizer = ^(BOOL isAdd){
            [self removeGesturRec:isAdd];
        };
    }
    return self;
}

//设置手势是否存在
- (void) removeGesturRec:(BOOL)isAdd{
    UINavigationController *centerNC = self.childViewControllers.lastObject;
    if (isAdd) {
        [centerNC.view removeGestureRecognizer:_rightSwipeGesture];
    }else{
        [centerNC.view addGestureRecognizer:_rightSwipeGesture];
    }

}


/**
 * 设置背景图片
 */
- (UIImageView *)backimage {
    
    if (!backimage) {
        backimage = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        UIImage *sourceImage = [UIImage imageNamed:@"黑豹"];
        UIImage *lastImage = [sourceImage applyDarkEffect];
        backimage.image = lastImage;
    }
    return backimage;
}

//响应右滑手势
- (void)leftAction{
  
    UINavigationController *centerNC = self.childViewControllers.lastObject;
    DrawerViewController *leftVC = self.childViewControllers.firstObject;
    [UIView animateWithDuration:0.5 animations:^{
        //返回
        if ( centerNC.view.center.x != self.view.center.x ) {
            leftVC.view.frame = CGRectMake(0, 0, 250, [UIScreen mainScreen].bounds.size.height);
            centerNC.view.frame = [UIScreen mainScreen].bounds;
            if (layerBack) {
                [layerBack removeFromSuperview];
                _rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftAction)];
                // 设置轻扫手势支持的方向
                _rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
                [centerNC.view addGestureRecognizer:_rightSwipeGesture];
            }
            return;
        }{
            //推出 主界面上有一层蒙板，上面有一个点击手势，和向左轻扫 响应leftAction事件
            centerNC.view.frame = CGRectMake(KSWidth/5*4, 0, KSWidth, KSHeight);
            _SwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftAction)];
            _SwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
            layerBack = [[UIView alloc]initWithFrame:centerNC.view.bounds];
            
            _tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(leftAction)];
            layerBack.alpha = 0.4;
            
            [layerBack addGestureRecognizer:_SwipeRecognizer];
            [layerBack addGestureRecognizer:_tapRecognizer];
            [centerNC.view addSubview:layerBack];
            [centerNC.view removeGestureRecognizer:_rightSwipeGesture];
        }
    }];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer{
    
    return YES;
}

//跳转到登录界面
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    LoginRegisterViewController *login = [[LoginRegisterViewController alloc]init];
    [self presentViewController:login animated:YES completion:nil];
    
}
@end
